<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/dataCharge.css" rel="stylesheet" type="text/css"/>
<title>Home</title>


</head>

<body>

<?php
	$user = $_POST['user'];
	echo "<div class='header'><a class='welcome'>Bienvenido usuario $user </a><div class='loginBox'><a href='index.php'>Cerrar sesión</a></div></div>";
?>
	
    
<div class="container">
     <br /><br /> 

<?php
	include_once('functions/dataBaseConnection.php');
	$succesfull = true;
	//carga de un nuevo proyecto
	if (isset($_POST['project'])) {
		$date = date("Y-m-d"); 	
		$code = $_POST['code'];
		$name = $_POST['name'];
		$query = "INSERT INTO  projects (code, name, date) VALUES ('$code', '$name' , '$date')";
		$result = mysqli_query($connection, $query);
		if (!$result) {
			$succesfull = false;
		}
	}
	
	//carga de una nueva consultoria
	
	if (isset($_POST['consultancy'])) {
		$date = date("Y-m-d"); 	
		$code = $_POST['code'];
		$name = $_POST['name'];
		$query = "INSERT INTO  consultancies (code, name, date) VALUES ('$code', '$name' , '$date')";
		$result = mysqli_query($connection, $query);
		if (!$result) {
			$succesfull = false;
		}
	}
	
	//carga de un nuevo recurso
	
	if (isset($_POST['resource'])) {
		$name = $_POST['name'];
		$surname = $_POST['surname'];
		$dni = $_POST['dni'];
		$mail = $_POST['mail'];
		$salary = $_POST['salary'];
		$user = $name . $dni;
		$pass = $name . $dni;
		$query = "INSERT INTO  users (user, pass, name, surname, dni, mail, salary, updated ) VALUES ('$user', '$pass' , '$name', '$surname', '$dni', '$mail' , '$salary', '1')";
		$result = mysqli_query($connection, $query);
		if (!$result) {
			$succesfull = false;
		}else{
			include_once('functions/sendMail.php');
			newUserMail($mail, $user, $pass);
		}
	}
	//modificación de un recurso
	
	if (isset($_POST['modifyResource'])) {
		$name = $_POST['name'];
		$surname = $_POST['surname'];
		$dni = $_POST['dni'];
		$mail = $_POST['mail'];
		$salary = $_POST['salary'];
		$query = "UPDATE users SET name= '$name', surname= '$surname', dni= '$dni', mail= '$mail', salary= '$salary' WHERE user='$user'";
		$result = mysqli_query($connection, $query);
		if (!$result) {
			$succesfull = false;
		}
	}
	//confirmacion de carga exitosa
	if ($succesfull) {
		echo "<a class='greenText'>Operaci&oacuten exitosa</a>";
	}else{
		echo "<a class='redText'>Error en la carga. Reintente</a>";
	}		
?>
	<input value="volver" name="back" type="button" onclick="document.location.href = 'home.php'" />
</div>

</body>
</html>